package com.dfd.store.service.impl;

import java.util.List;

import com.dfd.store.dao.ProductDao;
import com.dfd.store.dao.impl.ProductDaoImpl;
import com.dfd.store.domain.PageModel;
import com.dfd.store.domain.Product;
import com.dfd.store.service.ProductService;
import com.dfd.store.utils.BeanFactory;

public class ProductServiceImpl implements ProductService {
	ProductDao productdao = (ProductDao) BeanFactory.getName("ProductDao");

	@Override
	public List<Product> findHots() throws Exception {
		List<Product> hotslist = productdao.findHots();
		return hotslist;
	}

	@Override
	public List<Product> findNews() throws Exception {
		List<Product> newslist = productdao.findNews();
		return newslist;
	}

	@Override
	public Product findProductById(String pid) throws Exception {
		Product product = productdao.findProductById(pid);
		return product;
	}

	@Override
	public PageModel findProductByCidWithPage(String cid, int num) throws Exception {
		// 计算分页参数
		// 关联集合
		// 关联url
		int totalRecords = productdao.findTotalRecords(cid);
		// public PageModel(int currentPageNum, int pageSize, int totalRecords)
		// {
		PageModel pm = new PageModel(num, 10, totalRecords);
		// 关联集合 select * from product where cid = ? limit ?,?;
		List productList = productdao.findProductByCidWithPage(cid, pm.getStartIndex(), pm.getPageSize());
		pm.setList(productList);
		// 关联url
		pm.setUrl("ProductServlet?method=findProductByCidWithPage&cid=" + cid);
		return pm;
	}

	@Override
	public PageModel findAll(int num) throws Exception {
		// 计算分页参数
		int totalRecords = productdao.findTotalRecords();
		// 创建pm对象
		PageModel pageModel = new PageModel(num, 10, totalRecords);
		// 关联集合
		List productlist = productdao.findProductByCidWithPage(pageModel.getStartIndex(), pageModel.getPageSize());
		pageModel.setList(productlist);
		// 关联url
		pageModel.setUrl("AdminProductServlet?method=findAll");
		return pageModel;
	}

	@Override
	public PageModel findDown(int num) throws Exception {
		// 计算分页参数
		int totalRecords = productdao.findTotalRecords();
		// 创建pm对象
		PageModel pageModel = new PageModel(num, 10, totalRecords);
		// 关联集合
		List productlist = productdao.findDownWithPage(pageModel.getStartIndex(), pageModel.getPageSize());
		pageModel.setList(productlist);
		// 关联url
		pageModel.setUrl("AdminProductServlet?method=findDown");
		return pageModel;
	}

	@Override
	public void UpProduct(String pid) throws Exception {
		productdao.UpProduct(pid);
	}

	@Override
	public void addProduct(Product product) throws Exception {
		productdao.addProduct(product);
	}

	@Override
	public void DownProductByid(String pid) throws Exception {
		productdao.DownProductByid(pid);
	}

}
